package com.mtsmda.jpa.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "AUTHORS")
@NamedQuery(name = "Author.getAll", query = "SELECT a FROM Author a")
public class Author {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID_AUTHOR", nullable = false)
	private Integer idAuthor;

	@Column(name = "FIRSTNAME_AUTHOR", nullable = false, length = 50)
	private String firstnameAuthor;

	@Column(name = "LASTNAME_AUTHOR", nullable = false, length = 50)
	private String lastnameAuthor;

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinTable(name = "AUTHOR_BOOK", joinColumns = @JoinColumn(name = "BOOK_ID"), inverseJoinColumns = @JoinColumn(name = "AUTHOR_ID"))
	private List<Book> books = new ArrayList<>();

	public Author() {

	}

	public Author(String firstnameAuthor, String lastnameAuthor) {
		setFirstnameAuthor(firstnameAuthor);
		setLastnameAuthor(lastnameAuthor);
	}

	public Integer getIdAuthor() {
		return idAuthor;
	}

	public void setIdAuthor(Integer idAuthor) {
		this.idAuthor = idAuthor;
	}

	public String getFirstnameAuthor() {
		return firstnameAuthor;
	}

	public void setFirstnameAuthor(String firstnameAuthor) {
		this.firstnameAuthor = firstnameAuthor;
	}

	public String getLastnameAuthor() {
		return lastnameAuthor;
	}

	public void setLastnameAuthor(String lastnameAuthor) {
		this.lastnameAuthor = lastnameAuthor;
	}

	public List<Book> getBooks() {
		return books;
	}

	public void setBooks(List<Book> books) {
		this.books = books;
	}

	@Override
	public String toString() {
		return "Author [idAuthor=" + idAuthor + ", firstnameAuthor="
				+ firstnameAuthor + ", lastnameAuthor=" + lastnameAuthor
				+ ", books=" + books + "]";
	}

}